package project

import (
	"fmt"
	"net/http"
	"serve/model"

	"github.com/gin-gonic/gin"
)

func RegisterRouter(r *gin.RouterGroup) {
	r.GET("/list", List)
	r.POST("/add", Add)
}

// List  获取列表
func List(c *gin.Context) {
	var db = model.DB
	var p []model.Project
	result := db.Order("created_at desc").Find(&p)
	if result.Error != nil {
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprint(result.Error)})
		return
	}
	c.JSON(http.StatusOK, gin.H{"data": p, "code": 200})
}

func Add(c *gin.Context) {
	var db = model.DB
	var p model.Project
	p.Name = c.PostForm("name")
	p.Descript = c.PostForm("descript")

	fmt.Println(c.PostForm("name"))
	if len(p.Name) == 0 {
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": "参数错误"})
		return
	}
	db.Create(&p)

	c.JSON(http.StatusOK, gin.H{"data": p, "code": 200})

}
